package com.mxprojects.constructors
{
	import com.mxprojects.gui.Styles;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	
	public class DrawBorder extends Sprite
	{
		public static const BOX_LINE_WIDTH:uint = 1;
		private static var setLineWidth:uint;
		
		public function DrawBorder(){}
		
		public static function create(objectToBox:DisplayObject, lineColour:int = -1, lineWidth:uint = BOX_LINE_WIDTH):Sprite {
			//create a box around the object
			var left:Sprite = new Sprite();
			var top:Sprite = new Sprite();
			var right:Sprite = new Sprite();
			var bottom:Sprite = new Sprite();
			var border:Sprite = new Sprite();
			border.name = "border";
			
			//check the colour out
			var drawColour:uint = Styles.colorToUint(".defaultBorder");
			if(lineColour >= 0){
				drawColour = lineColour;
			}
			
			left.graphics.beginFill(drawColour);
			left.graphics.drawRect(0, 0, lineWidth, objectToBox.height);
			left.graphics.endFill();
			left.name = "left";
			
			top.graphics.beginFill(drawColour);
			top.graphics.drawRect(0, 0, objectToBox.width, lineWidth);
			top.graphics.endFill();
			top.name = "top";
			
			right.graphics.beginFill(drawColour);
			right.graphics.drawRect(0, 0, lineWidth, objectToBox.height);
			right.graphics.endFill();
			right.x = objectToBox.width - lineWidth;
			right.name = "right";
			
			bottom.graphics.beginFill(drawColour);
			bottom.graphics.drawRect(0, 0, objectToBox.width, lineWidth);
			bottom.graphics.endFill();
			bottom.y = objectToBox.height - lineWidth;
			bottom.name = "bottom";
			
			border.addChild(left);
			border.addChild(top);
			border.addChild(right);
			border.addChild(bottom);
			
			setLineWidth = lineWidth;
			
			//disable the mouse
			border.mouseEnabled = false;
			
			return border;
		}
		
		public static function redraw(objectToRedrawBox:DisplayObjectContainer, currentBoxLocation:DisplayObjectContainer):void {
			//get the box and then move around the graphics
			var border:Sprite = currentBoxLocation.getChildByName("border") as Sprite;
			if(border != null){
				var left:Sprite = border.getChildByName("left") as Sprite;
				var top:Sprite = border.getChildByName("top") as Sprite;
				var right:Sprite = border.getChildByName("right") as Sprite;
				var bottom:Sprite = border.getChildByName("bottom") as Sprite;
				//trace(objectToRedraw["bg_mc"].width);
				left.height = objectToRedrawBox.height;
				
				top.width = objectToRedrawBox.width;
				
				right.x = objectToRedrawBox.width - setLineWidth;
				right.height = objectToRedrawBox.height;
				
				bottom.y = objectToRedrawBox.height - setLineWidth;
				bottom.width = objectToRedrawBox.width;
			}
		} 

	}
}